import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Require {
    private JDBCUtil instance;
    String requireID;//REQUIRE_ID（需求ID）
    int circleCount;//圈数
    int riseCount;//升轨圈数
    int fallCount;//降轨圈数
    int minWorkTime;//最短跟踪时长 秒
    int maxWorkTime;//最大跟踪时长 秒
    double minTopel;//最低仰角
    double maxTopel;//最高仰角
    String devMode;//设备工作模式
    int singleTask;//同圈跟踪站数
    double minArcInterval;//最小弧段间隔
    double maxArcInterval;//最大弧段间隔
    int followAllArc;//全弧段跟踪
    int heoOrLeo;//1为低轨，2为高轨
    int deleted;//标记删除
    int requireType;//需求类型(应急 1，非应急 0)
    int priority;//优先级（0~99）
    Date startTime;//需求计划开始时间
    Date endTime;//需求计划结束时间
    int status;//需求状态,1:待审批,2: 已驳回,3: 待响应,4: 满足,5: 部分满足,6: 不满足, 7: 执行中, 8: 已完成
    String devGroupCode;//装备组代码
    String devGroup;//装备组
    int totalTask;//总任务数
    int completeTask;//已完成任务数

    ArrayList<String[]> subRequireData;//子需求数据集合，[子需求id，卫星，装备]
    public final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期时间格式

    public Require() {
        instance = JDBCUtil.getInstance();
        this.circleCount = 0;
        this.riseCount = 0;
        this.fallCount = 0;
        this.minWorkTime = 0;
        this.maxWorkTime = Integer.MAX_VALUE;
        this.minTopel = 0;
        this.maxTopel = 90;
        this.devMode = "";
        this.singleTask = 0;
        this.minArcInterval = 0;
        this.maxArcInterval = Double.MAX_VALUE;
        this.followAllArc = 0;
        this.heoOrLeo = 1;
        this.deleted = 0;
        this.requireType = 0;
        this.priority = 0;
        this.status = 3;
        this.devGroupCode = "";
        this.devGroup = "";
        this.totalTask = 0;
        this.completeTask = 0;
    }

    //加载并存储原始需求数据
    public void getRequire(String requireID) {
        System.out.println("获取需求数据：" + df.format(new Date()));
        this.requireID = requireID;
        this.subRequireData = new ArrayList<>();
        String sql = "SELECT * FROM xqcl_req_params where REQUIRE_ID = " + requireID;
        ResultSet rs = null;
        try {
            instance.getConnection();
            rs = instance.executeQuetyBYStatement(sql);
            //如果有数据，rs.next()返回true
            while (rs.next()) {
                //从数据中截取可用信息，获取需求
                this.circleCount = Integer.parseInt(rs.getString("CIRCLE_COUNT"));
                this.riseCount = Integer.parseInt(rs.getString("RISE_COUNT"));
                this.fallCount = Integer.parseInt(rs.getString("FALL_COUNT"));
                this.minWorkTime = Integer.parseInt(rs.getString("MIN_WORK_TIME"));
                this.maxWorkTime = Integer.parseInt(rs.getString("MAX_WORK_TIME"));
                this.minTopel = Double.parseDouble(rs.getString("MIN_TOPEL"));
                this.maxTopel = Double.parseDouble(rs.getString("MAX_TOPEL"));
                this.minArcInterval = Double.parseDouble(rs.getString("MIN_ARC_INTERVAL"));
                this.maxArcInterval = Double.parseDouble(rs.getString("MAX_ARC_INTERVAL"));
                this.devMode = rs.getString("DEV_MOUDLE");
                this.singleTask = Integer.parseInt(rs.getString("SINGLE_TASK"));
                this.deleted = Integer.parseInt(rs.getString("DELETED"));
                if (rs.getString("FOLLOW_ALL_ARC") != null) {
                    this.followAllArc = Integer.parseInt(rs.getString("FOLLOW_ALL_ARC"));
                }
                if (rs.getString("HEO_OR_LEO") != null) {
                    this.heoOrLeo = Integer.parseInt(rs.getString("HEO_OR_LEO"));
                }
            }
            sql = "SELECT * FROM xqcl_require where REQUIRE_ID = " + requireID;
            rs = instance.executeQuetyBYStatement(sql);
            while (rs.next()) {
                //从数据中截取可用信息，获取需求
                this.requireType = Integer.parseInt(rs.getString("REQUIRE_TYPE"));
                this.priority = Integer.parseInt(rs.getString("PRIORITY"));
                this.status = Integer.parseInt(rs.getString("STATUS"));
                this.startTime = df.parse(rs.getString("START_TIME"));
                this.endTime = df.parse(rs.getString("END_TIME"));
                this.devGroupCode = rs.getString("DEV_GROUP");
                this.completeTask = Integer.parseInt(rs.getString("COMPLETE_TASK"));
            }
            sql = "SELECT * FROM zbdd_device_group where GroupCode = \"" + devGroupCode + "\"";
            rs = instance.executeQuetyBYStatement(sql);
            while (rs.next()) {
                //获取装备组
                devGroup = rs.getString("DeviceCode");
            }
            sql = "SELECT * FROM xqcl_sub_require where REQUIRE_ID = " + requireID;
            rs = instance.executeQuetyBYStatement(sql);
            while (rs.next()) {
                String[] subRequire = new String[]{rs.getString("id"), rs.getString("SAT_CODE"), rs.getString("DEV_CODE")};
                this.subRequireData.add(subRequire);//子需求数据集合，[子需求id，卫星，装备]
            }
            this.totalTask = this.subRequireData.size();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("获取需求数据完成：" + df.format(new Date()));
    }

    //从数据库读取的需求数据可能存在缺失、空字段，或意义不明，需要对需求数据进行修正
    public void verifyRequire() {
        System.out.println("需求数据修正：" + df.format(new Date()));
        //若没限定最大工作时间
        if (this.maxWorkTime == 0) {
            this.maxWorkTime = Integer.MAX_VALUE;
        }
        //若没限定最大弧段间隔
        if (this.maxArcInterval == 0) {
            this.maxArcInterval = Double.MAX_VALUE;
        }
        //若对升轨圈数或降轨圈数有要求，以升降轨圈数为准
        if (this.riseCount > 0 || this.fallCount > 0) {
            this.circleCount = this.riseCount + this.fallCount;
        }
        System.out.println("需求数据修正完成：" + df.format(new Date()));
    }
}
